package com.efast.dmom.mgmt.controller.mes.qm;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mgmt.bean.mes.qm.QMCheckAreaBean;
import com.efast.dmom.mgmt.bean.mes.qm.QMCheckDefectCodeBean;
import com.efast.dmom.mgmt.bean.mes.qm.QMCheckDefectSaveBean;
import com.efast.dmom.mgmt.bean.mes.qm.QMCheckItemBean;
import com.efast.dmom.mgmt.bean.mes.qm.QMCheckPartBean;
import com.efast.dmom.mgmt.bean.mes.qm.QMDefectRequstBean;
import com.efast.dmom.mgmt.bean.mes.qm.QMRepairRecordSaveBean;
import com.efast.dmom.mgmt.service.mes.qm.impl.QMDefectService;

@Controller
@RequestMapping("/qm")
public class DefectMaintainController {

	@Autowired
	QMDefectService defectService;

	@RequestMapping(value = "/defect/getDefectArea/{stationId}")
	public @ResponseBody ResultBean getDefectArea(@PathVariable("stationId") Integer stationId) {

		ResultBean result = new ResultBean(true, null);

		List<QMCheckAreaBean> areaList = defectService.getCheckAreaByStationId(stationId);

		result.setObject(areaList);

		return result;
	}

	@RequestMapping(value = "/defect/getDefectPartsByAreaId/{areaId}")
	public @ResponseBody ResultBean getDefectPartsByAreaId(@PathVariable("areaId") Integer areaId) {

		ResultBean result = new ResultBean(true, null);

		List<QMCheckPartBean> partList = defectService.getCheckPartsByAreaId(areaId);

		result.setObject(partList);

		return result;
	}

	@RequestMapping(value = "/defect/getDefectCheckItemsByPartId/{partId}")
	public @ResponseBody ResultBean getDefectCheckItemsByPartId(@PathVariable("partId") Integer partId) {

		ResultBean result = new ResultBean(true, null);

		List<QMCheckItemBean> itemList = defectService.getCheckItemsByPartId(partId);

		result.setObject(itemList);

		return result;
	}

	@RequestMapping(value = "/defect/getDefectCodeByItemId/{itemId}")
	public @ResponseBody ResultBean getDefectCodeByItemId(@PathVariable("itemId") Integer itemId) {

		ResultBean result = new ResultBean(true, null);

		List<QMCheckDefectCodeBean> codeList = defectService.getDefectCodeByItemId(itemId);

		result.setObject(codeList);

		return result;
	}

	@RequestMapping(value = "/defect/saveDefectRecord")
	public @ResponseBody ResultBean saveDefectRecord(@RequestBody QMDefectRequstBean bean) {

		ResultBean result = new ResultBean(true, null);

		List<String> areaList = bean.getArea();
		List<String> partList = bean.getPart();
		List<String> checkItemList = bean.getCheckItem();
		List<String> defectCodeList = bean.getDefectCode();
		String lotName = bean.getLotName();
		String stationName = bean.getStation();
		String vin = bean.getVin();
		String selfNumber = bean.getSelfNumber();
		List<QMCheckDefectSaveBean> saveBeanList = new ArrayList<QMCheckDefectSaveBean>();

		if (areaList != null && areaList.size() > 0 && partList != null && partList.size() == areaList.size()
				&& checkItemList != null && checkItemList.size() == areaList.size() && defectCodeList != null
				&& defectCodeList.size() == areaList.size()) {

			for (int i = 0; i < areaList.size(); i++) {

				QMCheckDefectSaveBean saveBean = new QMCheckDefectSaveBean();

				saveBean.setArea(areaList.get(i));
				saveBean.setPart(partList.get(i));
				saveBean.setCheckItem(checkItemList.get(i));
				saveBean.setDefectCode(defectCodeList.get(i));
				saveBean.setLotName(lotName);
				saveBean.setStation(stationName);
				saveBean.setCreateTime(new Date());
				saveBean.setCreateUser(SpringWebUtils.getRequestUser());
				saveBean.setVin(vin);
				saveBean.setSelfNumber(selfNumber);
				saveBean.setRouteName(bean.getRouteName());
				saveBean.setOperationName(bean.getOperationName());
				saveBeanList.add(saveBean);
			}
		}
		if(saveBeanList.size() > 0){
			defectService.saveDefectRecord(saveBeanList);
		}
		
		return result;
	}

	@RequestMapping(value = "/defect/deleteDefectByDefectId/{id}")
	public @ResponseBody ResultBean deleteDefectByDefectId(@PathVariable("id") Integer id) {

		ResultBean result = new ResultBean(true, null);

		defectService.deleteDefectByDefectId(id);

		return result;
	}
	
	@RequestMapping(value = "/defect/saveRepairRecord")
	public @ResponseBody ResultBean saveRepairRecord(@RequestBody QMRepairRecordSaveBean saveBean) {

		ResultBean result = defectService.saveRepairRecord(saveBean);
		
		if(result.success){
			if("已关闭".equals(saveBean.getStatus())) {
				result.setSuccess(false);
				result.setMessage("请选择未关闭状态的车辆进行维修！");
			} else {
			defectService.RepairRecordStatus(saveBean.getId());
			}
		}
		
		return result;
	}
	
	@RequestMapping(value = "/defect/showDefectSection/{stationId}")
	public @ResponseBody ResultBean showDefectSection(@PathVariable("stationId") Integer stationId) {

		ResultBean result = new ResultBean(true, null);

		boolean toShow = defectService.showDefectSection(stationId);
		
		if(toShow){
			result.setObject("true");
		}
		
		return result;
	}
	
	@RequestMapping(value = "/defect/check")
	public @ResponseBody ResultBean checkDefectNum(@RequestBody Map<String,Object> vin) {


		 ResultBean result = defectService.selectRowNumByLot(vin);
		
		
		return result;
	}
}
